﻿/**  
* Form.cs
*
* 功 能： N/A
* 类 名： Form
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2014/6/29/星期日 21:25:31   N/A    初版
*
* Copyright (c) 2014  Spring Story  Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有： Spring Story 有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using eTruck.IDAL;
using eTruck.DBUtility;
namespace eTruck.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Form
	/// </summary>
	public partial class Form:IForm
	{
		public Form()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("FormID", "Form"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int FormID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Form");
			strSql.Append(" where FormID=@FormID");
			SqlParameter[] parameters = {
					new SqlParameter("@FormID", SqlDbType.Int,4)
			};
			parameters[0].Value = FormID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(eTruck.Model.Form model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Form(");
            strSql.Append("CarID,InDatetime,DriverID,CardID,FormTypeID,Contact,OrderNumber,CheckedCar,Noticed,OutDatetime,Processid,Reservation_In_Time,UserID)");
			strSql.Append(" values (");
            strSql.Append("@CarID,@InDatetime,@DriverID,@CardID,@FormTypeID,@Contact,@OrderNumber,@CheckedCar,@Noticed,@OutDatetime,@Processid,@Reservation_In_Time,@UserID)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@CarID", SqlDbType.Int,4),
					new SqlParameter("@InDatetime", SqlDbType.DateTime),
					new SqlParameter("@DriverID", SqlDbType.Int,4),
					new SqlParameter("@CardID", SqlDbType.VarChar,20),
					new SqlParameter("@FormTypeID", SqlDbType.Int,4),
					new SqlParameter("@Contact", SqlDbType.VarChar,20),
					new SqlParameter("@OrderNumber", SqlDbType.VarChar,20),
					new SqlParameter("@CheckedCar", SqlDbType.Bit,1),
					new SqlParameter("@Noticed", SqlDbType.Bit,1),
					new SqlParameter("@OutDatetime", SqlDbType.DateTime),
					new SqlParameter("@Processid", SqlDbType.Int,4),
                    new SqlParameter("@Reservation_In_Time", SqlDbType.DateTime),
                    new SqlParameter("@UserID", SqlDbType.Int,4)};
			parameters[0].Value = model.CarID;
			parameters[1].Value = model.InDatetime;
			parameters[2].Value = model.DriverID;
			parameters[3].Value = model.CardID;
			parameters[4].Value = model.FormTypeID;
			parameters[5].Value = model.Contact;
			parameters[6].Value = model.OrderNumber;
			parameters[7].Value = model.CheckedCar;
			parameters[8].Value = model.Noticed;
			parameters[9].Value = model.OutDatetime;
			parameters[10].Value = model.Processid;
            parameters[11].Value = model.Reservation_In_Time;
            parameters[12].Value = model.UserID;


			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(eTruck.Model.Form model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Form set ");
			strSql.Append("CarID=@CarID,");
			strSql.Append("InDatetime=@InDatetime,");
			strSql.Append("DriverID=@DriverID,");
			strSql.Append("CardID=@CardID,");
			strSql.Append("FormTypeID=@FormTypeID,");
			strSql.Append("Contact=@Contact,");
			strSql.Append("OrderNumber=@OrderNumber,");
			strSql.Append("CheckedCar=@CheckedCar,");
			strSql.Append("Noticed=@Noticed,");
			strSql.Append("OutDatetime=@OutDatetime,");
			strSql.Append("Processid=@Processid,");
            strSql.Append("Reservation_In_Time=@Reservation_In_Time,");
            strSql.Append("UserID=@UserID");
			strSql.Append(" where FormID=@FormID");
			SqlParameter[] parameters = {
					new SqlParameter("@CarID", SqlDbType.Int,4),
					new SqlParameter("@InDatetime", SqlDbType.DateTime),
					new SqlParameter("@DriverID", SqlDbType.Int,4),
					new SqlParameter("@CardID", SqlDbType.VarChar,20),
					new SqlParameter("@FormTypeID", SqlDbType.Int,4),
					new SqlParameter("@Contact", SqlDbType.VarChar,20),
					new SqlParameter("@OrderNumber", SqlDbType.VarChar,20),
					new SqlParameter("@CheckedCar", SqlDbType.Bit,1),
					new SqlParameter("@Noticed", SqlDbType.Bit,1),
					new SqlParameter("@OutDatetime", SqlDbType.DateTime),
					new SqlParameter("@Processid", SqlDbType.Int,4),
                    new SqlParameter("@Reservation_In_Time", SqlDbType.DateTime),
                    new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@FormID", SqlDbType.Int,4)};
			parameters[0].Value = model.CarID;
			parameters[1].Value = model.InDatetime;
			parameters[2].Value = model.DriverID;
			parameters[3].Value = model.CardID;
			parameters[4].Value = model.FormTypeID;
			parameters[5].Value = model.Contact;
			parameters[6].Value = model.OrderNumber;
			parameters[7].Value = model.CheckedCar;
			parameters[8].Value = model.Noticed;
			parameters[9].Value = model.OutDatetime;
			parameters[10].Value = model.Processid;
            parameters[11].Value = model.Reservation_In_Time;
            parameters[12].Value = model.UserID;
			parameters[13].Value = model.FormID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int FormID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Form ");
			strSql.Append(" where FormID=@FormID");
			SqlParameter[] parameters = {
					new SqlParameter("@FormID", SqlDbType.Int,4)
			};
			parameters[0].Value = FormID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string FormIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Form ");
			strSql.Append(" where FormID in ("+FormIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public eTruck.Model.Form GetModel(int FormID)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 FormID,CarID,InDatetime,DriverID,CardID,FormTypeID,Contact,OrderNumber,CheckedCar,Noticed,OutDatetime,Processid,Reservation_In_Time,UserID from Form ");
			strSql.Append(" where FormID=@FormID");
			SqlParameter[] parameters = {
					new SqlParameter("@FormID", SqlDbType.Int,4)
			};
			parameters[0].Value = FormID;

			eTruck.Model.Form model=new eTruck.Model.Form();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public eTruck.Model.Form DataRowToModel(DataRow row)
		{
			eTruck.Model.Form model=new eTruck.Model.Form();
			if (row != null)
			{
				if(row["FormID"]!=null && row["FormID"].ToString()!="")
				{
					model.FormID=int.Parse(row["FormID"].ToString());
				}
				if(row["CarID"]!=null && row["CarID"].ToString()!="")
				{
                    model.CarID = int.Parse(row["CarID"].ToString());
				}
				if(row["InDatetime"]!=null && row["InDatetime"].ToString()!="")
				{
					model.InDatetime=DateTime.Parse(row["InDatetime"].ToString());
				}
				if(row["DriverID"]!=null && row["DriverID"].ToString()!="")
				{
					model.DriverID=int.Parse(row["DriverID"].ToString());
				}
                if (row["CardID"] != null && row["CardID"].ToString() != "")
                {
                    model.CardID = row["CardID"].ToString();
                }
               
				if(row["FormTypeID"]!=null && row["FormTypeID"].ToString()!="")
				{
					model.FormTypeID=int.Parse(row["FormTypeID"].ToString());
				}
				if(row["Contact"]!=null)
				{
					model.Contact=row["Contact"].ToString();
				}
				if(row["OrderNumber"]!=null)
				{
					model.OrderNumber=row["OrderNumber"].ToString();
				}
				if(row["CheckedCar"]!=null && row["CheckedCar"].ToString()!="")
				{
					if((row["CheckedCar"].ToString()=="1")||(row["CheckedCar"].ToString().ToLower()=="true"))
					{
						model.CheckedCar=true;
					}
					else
					{
						model.CheckedCar=false;
					}
				}
				if(row["Noticed"]!=null && row["Noticed"].ToString()!="")
				{
					if((row["Noticed"].ToString()=="1")||(row["Noticed"].ToString().ToLower()=="true"))
					{
						model.Noticed=true;
					}
					else
					{
						model.Noticed=false;
					}
				}
				if(row["OutDatetime"]!=null && row["OutDatetime"].ToString()!="")
				{
					model.OutDatetime=DateTime.Parse(row["OutDatetime"].ToString());
				}
				if(row["Processid"]!=null && row["Processid"].ToString()!="")
				{
					model.Processid=int.Parse(row["Processid"].ToString());
				}
                if (row["Reservation_In_Time"] != null && row["Reservation_In_Time"].ToString() != "")
                {
                    model.Reservation_In_Time = DateTime.Parse(row["Reservation_In_Time"].ToString());
                }
                if (row["UserID"] != null && row["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(row["UserID"].ToString());
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select FormID,CarID,InDatetime,DriverID,CardID,FormTypeID,Contact,OrderNumber,CheckedCar,Noticed,OutDatetime,Processid,Reservation_In_Time,UserID ");
			strSql.Append(" FROM Form ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" FormID,CarID,InDatetime,DriverID,CardID,FormTypeID,Contact,OrderNumber,CheckedCar,Noticed,OutDatetime,Processid,Reservation_In_Time,UserID ");
			strSql.Append(" FROM Form ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Form ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.FormID desc");
			}
			strSql.Append(")AS Row, T.*  from Form T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Form";
			parameters[1].Value = "FormID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        public DataSet GetInMyQueue(string Processid,string KeyWord)
        {
            string sqlstr = @"select a.FormID,c.Car_NO, e.CorporationName,--e.Contact as Supplier_Contact,
                        b.DriverName,b.CertificationNO,d.FormType,a.Contact,a.OrderNumber,a.CardID,a.Reservation_In_Time
                         from Form a with(nolock),Driver b with(nolock),Car c with(nolock),FormTypes d with(nolock),
                         Supplier e with(nolock)
                        where a.DriverID=b.DriverID and a.CarID=c.CarID
                        and a.FormTypeID=d.FormTypeID and c.CorporationID=e.CorporationID";
            sqlstr = sqlstr + " and a.Processid in (" + Processid.ToString()+")";
            if (KeyWord != "")
            {
                KeyWord = KeyWord.Replace("'", "");
                sqlstr = sqlstr + " and Car_NO like  '%" + KeyWord + "%'";
            }
            return DbHelperSQL.Query(sqlstr.ToString());
        }
		#endregion  ExtensionMethod
	}
}

